package com.zygh.coordinatestore.util;

/**
 * 对接pgis类型转换类
 */
public class BitConverter {

    public static String byteArrayToString(byte[] data) {
        StringBuilder sb = new StringBuilder();
        for (byte b : data) {
            sb.append(String.format("%02X", b));
        }
        return sb.toString();
    }

    public static byte[] StringToByteArray(String data) {
        int NumberChars = data.length();
        byte[] bytes = new byte[NumberChars / 2];
        for (int i = 0; i < NumberChars; i += 2)
            bytes[i / 2] = (byte) Integer.valueOf(data.substring(i, i + 2), 16)
                    .intValue();
        return bytes;
    }

    /**
     * 把double型数据转换成byte类型
     *
     * @param d
     * @return
     */
    public static byte[] doubleToByte(double d) {
        byte[] b = new byte[8];
        long l = Double.doubleToLongBits(d);
        for (int i = 0; i < b.length; i++) {
            b[i] = new Long(l).byteValue();
            l = l >> 8;
        }
        return b;
    }

    /**
     * 把int型数据转换成byte类型
     *
     * @param number
     * @return
     */
    public static byte[] intToByte(int number) {
        int temp = number;
        byte[] b = new byte[4];
        for (int i = b.length - 1; i > -1; i--) {
            b[i] = new Integer(temp & 0xff).byteValue(); // 将最高位保存在最低位
            temp = temp >> 8; // 向右移8位
        }
        return b;
    }

    /**
     * 把byte型数据转换成long类型
     *
     * @param bb
     * @param index
     * @return
     */
    public static long getLong(byte[] bb, int index) {
        return ((((long) bb[index + 0] & 0xff) << 56)
                | (((long) bb[index + 1] & 0xff) << 48)
                | (((long) bb[index + 2] & 0xff) << 40)
                | (((long) bb[index + 3] & 0xff) << 32)
                | (((long) bb[index + 4] & 0xff) << 24)
                | (((long) bb[index + 5] & 0xff) << 16)
                | (((long) bb[index + 6] & 0xff) << 8) | (((long) bb[index + 7] & 0xff) << 0));
    }

    public static int byteArrayToInt(byte[] b) {
        int b0 = (b[0] & 0x000000FF) << 24;
        int b1 = (b[1] & 0x000000FF) << 16;
        int b2 = (b[2] & 0x000000FF) << 8 ;
        int b3 = (b[3] & 0x000000FF);
        return b0+b1+b2+b3;
    }

    public static short byteArrayToShort(byte[] b) {
        int b0 = b[1] & 0x000000FF  ;
        int b1 = (b[0] & 0x000000FF )<< 8;
        short s = new Integer(b0 + b1).shortValue();
        return s;
    }

    public static double byteToDouble(byte[] b) {
        long l;

        l = b[0];
        l &= 0xff;
        l |= ((long) b[1] << 8);
        l &= 0xffff;
        l |= ((long) b[2] << 16);
        l &= 0xffffff;
        l |= ((long) b[3] << 24);
        l &= 0xffffffffl;
        l |= ((long) b[4] << 32);
        l &= 0xffffffffffl;

        l |= ((long) b[5] << 40);
        l &= 0xffffffffffffl;
        l |= ((long) b[6] << 48);

        l |= ((long) b[7] << 56);
        return Double.longBitsToDouble(l);
    }

    // 字符到字节转换
    public static byte[] charToByte(char ch) {
        int temp = (int) ch;
        byte[] b = new byte[2];
        for (int i = b.length - 1; i > -1; i--) {
            b[i] = new Integer(temp & 0xff).byteValue();// 将最高位保存在最低位
            temp = temp >> 8;// 向右移8位
        }
        return b;
    }

    // 字节到字符转换

    public static char byteToChar(byte[] b) {
        int s = 0;
        if (b[0] > 0)
            s += b[0];
        else
            s += 256 + b[0];
        s *= 256;
        if (b[1] > 0)
            s += b[1];
        else
            s += 256 + b[1];
        char ch = (char) s;
        return ch;
    }

    /**
     * 低字节数组到short的转换
     * @param b byte[]
     * @return short
     */
    public static short byteToShort(byte[] b) {
        int s=0;
        if(b[0]>=0){
            s = s + b[0];
        }else{
            s = s + 256 + b[0];
        }
        s = s * 256;
        if  (b[ 1 ] >= 0){
            s = s + b[1];
        }else{
            s=s+256+b[1];
        }
        short result=(short)s;
        return result;
    }

    /*public static void main(String[] args) {
        byte[] year = new byte[2];
        year[0]= (byte) 0x07;
        year[1]= (byte) 0xE5;
        System.out.println( BitConverter.byteToShort( year ) );

        System.out.println(0xE507);
        System.out.println(0x07E5);
        System.out.println(0xAA);

    }*/

}
